package Controls
{
	import Native.NativeField;
	
	import Tools.DisplayTool;
	import Input.GameInput;
	
	import flash.geom.Rectangle;
	import flash.text.TextLineMetrics;

	public class VScrollBar extends ScrollBar
	{
		public function VScrollBar()
		{
			super();
		}
		override protected function syncNormalScroll():void
		{
			var bounds:Rectangle=target.obj.transform.pixelBounds;
			var height:Number=visibleRect.height/bounds.height*scrollRect.height;
			height=Math.max(20,height);
			slider.obj.scaleY=height/slider.originalHeight;
			dragRect.height=scrollRect.height-slider.obj.height;
			visibleRect.y=(slider.obj.y-scrollRect.y)/(scrollRect.height-slider.obj.height)*(bounds.height-visibleRect.height);
			target.obj.scrollRect=visibleRect;
		}
		override protected function syncFieldScroll():void
		{
			var numLines:int=NativeField(target.obj).numLines;
			var maxScrollV:int=NativeField(target.obj).maxScrollV;
			var height:Number=(1-maxScrollV/numLines)*scrollRect.height;
			height=Math.max(20,height);
			slider.obj.scaleY=height/slider.originalHeight;
			dragRect.height=scrollRect.height-slider.obj.height;
			var scrollV:int=Math.ceil((slider.obj.y-scrollRect.y)/(scrollRect.height-slider.obj.height)*maxScrollV);
			NativeField(target.obj).scrollV=scrollV;
		}
		override protected function doArrow1Command():void
		{
			slider.obj.y-=arrowStep;
			lockSlider();
		}
		override protected function doArrow2Command():void
		{
			slider.obj.y+=arrowStep;
			lockSlider();
		}
		override protected function lockSlider():void
		{
			slider.obj.x=scrollRect.x;
			slider.obj.y=Math.min(slider.obj.y,scrollRect.y+scrollRect.height-slider.obj.height);
			slider.obj.y=Math.max(slider.obj.y,scrollRect.y);
		}
		override public function setup(width:Number, height:Number):void
		{
			track.scale(width,height);
			arrow1.moveTo(width/2,0,0.5,0);
			arrow2.moveTo(width/2,height,0.5,1);
			
			scrollRect.x=width/2-slider.obj.width/2;
			scrollRect.y=arrow1.obj.height;
			scrollRect.width=slider.obj.width;
			scrollRect.height=height-arrow1.obj.height-arrow2.obj.height;
			
			slider.obj.x=scrollRect.x;
			slider.obj.y=scrollRect.y;
			
			dragRect.x=scrollRect.x;
			dragRect.width=0;
			dragRect.y=scrollRect.y;
			dragRect.height=scrollRect.height-slider.obj.height;
		}
		override public function scrollPercent(percent:Number):void
		{
			slider.obj.x=(scrollRect.height-slider.obj.height)*percent;
			lockSlider();
		}
	}
}